package com.sys.SysTanks.service.impl;

import java.util.Date;
import java.util.List;

import com.sys.SysTanks.domain.SysTanks;
import com.sys.SysTanks.mapper.SysTanksMapper;
import com.sys.common.exception.ServiceException;
import com.sys.common.utils.DateUtils;
import com.sys.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sys.SysTanks.mapper.SysTanksAreaMapper;
import com.sys.SysTanks.domain.SysTanksArea;
import com.sys.SysTanks.service.ISysTanksAreaService;

/**
 * 养殖桶区域Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-08
 */
@Service
public class SysTanksAreaServiceImpl implements ISysTanksAreaService 
{
    @Autowired
    private SysTanksAreaMapper sysTanksAreaMapper;


    @Autowired
    private SysTanksMapper sysTanksMapper;

    /**
     * 查询养殖桶区域
     * 
     * @param id 养殖桶区域主键
     * @return 养殖桶区域
     */
    @Override
    public SysTanksArea selectSysTanksAreaById(Long id)
    {
        return sysTanksAreaMapper.selectSysTanksAreaById(id);
    }

    /**
     * 查询养殖桶区域列表
     * 
     * @param sysTanksArea 养殖桶区域
     * @return 养殖桶区域
     */
    @Override
    public List<SysTanksArea> selectSysTanksAreaList(SysTanksArea sysTanksArea)
    {
        return sysTanksAreaMapper.selectSysTanksAreaList(sysTanksArea);
    }

    /**
     * 新增养殖桶区域
     * 
     * @param sysTanksArea 养殖桶区域
     * @return 结果
     */
    @Override
    public int insertSysTanksArea(SysTanksArea sysTanksArea)
    {
        sysTanksArea.setCreatedTime(DateUtils.getNowDate());
        sysTanksArea.setCreatedBy(SecurityUtils.getUsername());
        sysTanksArea.setUpdatedTime(DateUtils.getNowDate());
        sysTanksArea.setUpdatedBy(SecurityUtils.getUsername());
        return sysTanksAreaMapper.insertSysTanksArea(sysTanksArea);
    }

    /**
     * 修改养殖桶区域
     * 
     * @param sysTanksArea 养殖桶区域
     * @return 结果
     */
    @Override
    public int updateSysTanksArea(SysTanksArea sysTanksArea)
    {
        Date now = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        int updated = 0;

        // ========== 情况1：批量逻辑删除 ==========
        if (sysTanksArea.getIds() != null && !sysTanksArea.getIds().isEmpty()) {
            List<Long> areaIds = sysTanksArea.getIds();

            // 检查这些区域下是否存在非空桶
            int count = sysTanksMapper.countByAreaIdsAndStatus(areaIds);
            if (count > 0) {
                throw new ServiceException("共存在 " + count + " 个养殖桶分布在待删除的区域中，无法删除");
            }

            // 批量更新状态为 0（逻辑删除）
            for (Long areaId : areaIds) {
                SysTanksArea area = new SysTanksArea();
                area.setId(areaId);
                area.setStatus(0L);
                area.setUpdatedTime(now);
                area.setUpdatedBy(username);
                updated += sysTanksAreaMapper.updateSysTanksArea(area);
            }
            return updated;
        }

        // ========== 情况2：单个逻辑删除（status = 0）==========
        if (sysTanksArea.getStatus() != null && sysTanksArea.getStatus() == 0) {
            Long id = sysTanksArea.getId();
            if (id == null) {
                throw new IllegalArgumentException("删除操作必须提供区域 ID");
            }

            // 检查该区域下是否有非空桶
            SysTanks sysTanks = new SysTanks();
            sysTanks.setAreaId(id);
            sysTanks.setStatus("空桶");
//            List<SysTanks> list = sysTanksMapper.selectSysTanksList(sysTanks);
            int count = sysTanksMapper.countByCondition(sysTanks);
//            int count = sysTanksMapper.countByCondition(id);
            if (count > 0) {
                throw new ServiceException("该区域下存在 " + count + " 个非空养殖桶，无法删除");
            }
        }

        // ========== 情况3：单个更新或删除 ==========
        sysTanksArea.setUpdatedTime(now);
        sysTanksArea.setUpdatedBy(username);
        return sysTanksAreaMapper.updateSysTanksArea(sysTanksArea);
    }

    /**
     * 批量删除养殖桶区域
     * 
     * @param ids 需要删除的养殖桶区域主键
     * @return 结果
     */
    @Override
    public int deleteSysTanksAreaByIds(Long[] ids)
    {
        return sysTanksAreaMapper.deleteSysTanksAreaByIds(ids);
    }


    /**
     * 删除养殖桶区域信息
     * 
     * @param id 养殖桶区域主键
     * @return 结果
     */
    @Override
    public int deleteSysTanksAreaById(Long id)
    {
        return sysTanksAreaMapper.deleteSysTanksAreaById(id);
    }
}
